<?php
/**
 * 测试转赠卡片功能
 */

require_once __DIR__ . '/backend/support/bootstrap.php';

use app\model\CardUserModel;
use app\model\UserModel;
use app\model\CardModel;
use app\model\CardItemModel;

// 模拟测试数据
echo "=== 转赠卡片功能测试 ===\n";

// 1. 创建测试用户
echo "1. 创建测试用户...\n";
$sourceUser = new UserModel();
$sourceUser->open_id = 'test_source_user_' . time();
$sourceUser->phone = '13800138001';
$sourceUser->nickname = '源用户';
$sourceUser->created_at = time();
$sourceUser->updated_at = time();
$sourceUser->save();

$targetUser = new UserModel();
$targetUser->open_id = 'test_target_user_' . time();
$targetUser->phone = '13800138002';
$targetUser->nickname = '目标用户';
$targetUser->created_at = time();
$targetUser->updated_at = time();
$targetUser->save();

echo "源用户ID: {$sourceUser->id}, 手机号: {$sourceUser->phone}\n";
echo "目标用户ID: {$targetUser->id}, 手机号: {$targetUser->phone}\n";

// 2. 创建测试卡片
echo "\n2. 创建测试卡片...\n";
$card = new CardModel();
$card->name = '测试主题卡';
$card->type = 'topic';
$card->score = 100;
$card->price = 10.00;
$card->expired_days = 365;
$card->created_at = time();
$card->updated_at = time();
$card->save();

echo "卡片ID: {$card->id}, 名称: {$card->name}\n";

// 3. 创建卡片项
echo "\n3. 创建卡片项...\n";
$cardItem = new CardItemModel();
$cardItem->card_id = $card->id;
$cardItem->card_number = CardItemModel::generateCardNumber();
$cardItem->card_no = CardItemModel::generateCardNo();
$cardItem->status = CardItemModel::STATUS_UNUSED;
$cardItem->expired_days = $card->expired_days;
$cardItem->score = $card->score;
$cardItem->created_at = time();
$cardItem->updated_at = time();
$cardItem->save();

echo "卡片项ID: {$cardItem->id}, 卡号: {$cardItem->card_number}\n";

// 4. 创建用户卡片绑定
echo "\n4. 创建用户卡片绑定...\n";
$cardUser = new CardUserModel();
$cardUser->user_id = $sourceUser->id;
$cardUser->card_id = $card->id;
$cardUser->card_item_id = $cardItem->id;
$cardUser->status = CardUserModel::STATUS_ACTIVE;
$cardUser->score = $cardItem->score;
$cardUser->created_at = time();
$cardUser->updated_at = time();
$cardUser->phone = $sourceUser->phone;
$cardUser->type = 'topic';
$cardUser->save();

echo "卡片用户绑定ID: {$cardUser->id}\n";

// 5. 模拟转赠操作
echo "\n5. 模拟转赠操作...\n";

// 验证源用户是否拥有该卡片
$checkCardUser = CardUserModel::where('id', $cardUser->id)
    ->where('user_id', $sourceUser->id)
    ->where('status', CardUserModel::STATUS_ACTIVE)
    ->first();

if (!$checkCardUser) {
    echo "错误: 源用户不拥有该卡片\n";
    exit;
}

// 验证目标用户是否存在
$checkTargetUser = UserModel::where('phone', $targetUser->phone)->first();
if (!$checkTargetUser) {
    echo "错误: 目标用户不存在\n";
    exit;
}

// 执行转赠
$cardUser->user_id = $targetUser->id;
$cardUser->phone = $targetUser->phone;
$cardUser->updated_at = time();
$cardUser->save();

echo "转赠成功！\n";

// 6. 验证转赠结果
echo "\n6. 验证转赠结果...\n";
$transferredCard = CardUserModel::where('id', $cardUser->id)->first();
echo "转赠后用户ID: {$transferredCard->user_id}\n";
echo "转赠后手机号: {$transferredCard->phone}\n";

// 验证源用户是否还有该卡片
$sourceUserCard = CardUserModel::where('id', $cardUser->id)
    ->where('user_id', $sourceUser->id)
    ->first();

if ($sourceUserCard) {
    echo "错误: 源用户仍然拥有该卡片\n";
} else {
    echo "验证成功: 源用户已不再拥有该卡片\n";
}

// 验证目标用户是否拥有该卡片
$targetUserCard = CardUserModel::where('id', $cardUser->id)
    ->where('user_id', $targetUser->id)
    ->first();

if ($targetUserCard) {
    echo "验证成功: 目标用户现在拥有该卡片\n";
} else {
    echo "错误: 目标用户未获得该卡片\n";
}

echo "\n=== 测试完成 ===\n"; 